package cn.ecut.dzy.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @program: juc
 * @description://演示读写锁降级
 * @author: DingZhenYun
 * @create: 2021-10-19 14:23
 **/

public class Demo1 {
    public static void main(String[] args) {
        //可重入读写锁对象
        ReentrantReadWriteLock readWriteLock=new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();//读锁
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();//写锁
        //锁降级
        //1 获取写锁
        writeLock.lock();
        System.out.println("dzy");

        //2 获取读锁
        readLock.lock();
        System.out.println("---read");



        //3 释放写锁
        writeLock.unlock();

        //4 释放读锁
        readLock.unlock();
    }
}
